﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Gedea.WinForm.Helpers;
using Gedea.Model.Entities;
using Gedea.Service.Services;
using Gedea.WinForm.Resources.DBObjects;

namespace Gedea.WinForm.Forms.Areas
{
    public partial class Search : Form
    {
        private delegate void Deleg1();
        private delegate void Deleg2(Area area);
        private List<Area> areas = new AreaService().List();

        public static Search Instance
        {
            get { return ViewLocator<Search>.Instance; }
        }

        private Search()
        {
            InitializeComponent();
        }

        private void Settings_FormClosed(object sender, FormClosedEventArgs e)
        {
            ViewLocator<Search>.CloseInstance = null;
        }

        private void Search_Load(object sender, EventArgs e)
        {
            RadioButtons_Load();

            ComboBoxs_Load();

            UserControls.AreaControl viewUser = new UserControls.AreaControl(new Deleg1(CloseApp), new Deleg2(EditArea), true);
            viewUser.AutoSize = true;
            panel1.Controls.Add(viewUser);

            BlockFields(false);


        }

        private void ComboBoxs_Load()
        {
            comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;

            CustomizeForField(GetCurrentRadioButton().Text);

            comboBox1.ValueMember = "AreaId";
        }

        private void CustomizeForField(string fieldString)
        {
            Gedea.Model.DBObjects.Areas field = new Model.DBObjects.Areas();
            List<Area> data = new List<Area>();

            if (fieldString == AreaStrings.ResourceManager.GetString(Gedea.Model.DBObjects.Areas.name.ToString()))
            {
                field = Gedea.Model.DBObjects.Areas.name;
                data = areas.OrderBy(x => x.Name).ToList();
            }
            else if (fieldString == AreaStrings.ResourceManager.GetString(Gedea.Model.DBObjects.Areas.debitUnivocalReference.ToString()))
            {
                field = Gedea.Model.DBObjects.Areas.debitUnivocalReference;
                data = areas.OrderBy(x => x.DebitUnivocalReference).ToList();
            }
            comboBox1.DisplayMember = field.ToString();
            comboBox1.DataSource = data;
        }

        private void RadioButtons_Load()
        {
            radioButton1.Text = AreaStrings.ResourceManager.GetString(Gedea.Model.DBObjects.Areas.name.ToString());
            radioButton3.Text = AreaStrings.ResourceManager.GetString(Gedea.Model.DBObjects.Areas.debitUnivocalReference.ToString());
        }

        private RadioButton GetCurrentRadioButton()
        {
            return Instance.groupBox1.Controls.OfType<RadioButton>().FirstOrDefault(r => r.Checked);
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            CustomizeForField(GetCurrentRadioButton().Text);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Area area = areas.Where(x => x.AreaId == Convert.ToInt32(comboBox1.SelectedValue)).FirstOrDefault();
            if (area == null)
            {
                MessageBox.Show("No se encontró el área buscada");
            }
            else
            {
                UserControls.AreaControl control = (UserControls.AreaControl)panel1.Controls[0];
                control.myControls[0].Text = area.Name;
                control.myControls[1].Text = area.DebitUnivocalReference;
                BlockFields(true);
            }

        }

        private void CloseApp()
        {
            this.Close();
        }

        private void EditArea(Area area)
        {
            area.AreaId = Convert.ToInt32(comboBox1.SelectedValue);
            new AreaService().Update(area);
            MessageBox.Show("El área ha sido modificada con éxito");
            this.Close();
        }

        private void BlockFields(bool resp)
        {
            UserControls.AreaControl control = (UserControls.AreaControl)panel1.Controls[0];
            control.myControls[0].Enabled = resp;
            control.myControls[1].Enabled = resp;
        }
    }
}
